# clear environment
rm(list=ls())

# load packages
library(ggplot2)

# load data set on intelligence reports
dat = openxlsx::read.xlsx("vsberichte_metadata.xlsx", sheet = 1)

# remove reports without information on chater start
reps <- dat[!is.na(dat$page_right_start) | !is.na(dat$page_left_start),]

# rename jurisdiction variable
reps$jurisdiction <- tools::toTitleCase(reps$jurisdiction)
reps$jurisdiction[reps$jurisdiction=="Baden-Württemberg"] <- "Baden-Wuerttemberg"
reps$jurisdiction[reps$jurisdiction=="Bayern"] <- "Bavaria"
reps$jurisdiction[reps$jurisdiction=="Bund"] <- "Federal"
reps$jurisdiction[reps$jurisdiction=="Nordrhein-Westfalen"] <- "North Rhine-Westphalia"
reps$jurisdiction[reps$jurisdiction=="Sachsen"] <- "Saxony"
reps$jurisdiction[reps$jurisdiction=="Sachsen-Anhalt"] <- "Saxony-Anhalt"
reps$jurisdiction[reps$jurisdiction=="Thüringen"] <- "Thuringia"
reps$jurisdiction <- factor(reps$jurisdiction)
reps$jurisdiction <- factor(reps$jurisdiction, levels=rev(levels(reps$jurisdiction)))

# code center-right interior minister
reps$cr_intmin <- ifelse(reps$intmin_party == "cdu" | reps$intmin_party== "csu" | reps$intmin_party=="fdp", 1, ifelse(reps$intmin_party=="pro", 2, 0))

# create figure
ggplot(data=reps[reps$year>=1964,], aes(x=jurisdiction, y=year, color=factor(cr_intmin))) + 
  coord_flip() + 
  geom_point() +
  theme_classic() +
  theme(legend.position = "bottom", text=element_text(size=12)) +
  xlab("Jurisdiction") + ylab("Year") +
  scale_y_continuous(limits=c(1960, 2025), breaks=seq(1960, 2025, 10)) +
  scale_color_manual(values=c("firebrick", "dodgerblue", "darkgreen"), labels=c("Center-Left", "Center-Right", "Right-Wing\nPopulist"), name="Interior Minister Party")

# save figure
ggsave("FigD3.pdf", width=7, height=5)


